package com.imachen.pet.service.impl;

import com.imachen.basic.service.IBaiduAiAuditService;
import com.imachen.basic.service.impl.BaseServiceImpl;
import com.imachen.basic.utils.AjaxResult;
import com.imachen.pet.domain.Pet;
import com.imachen.pet.domain.PetDetail;
import com.imachen.pet.domain.PetOnlineAuditLog;
import com.imachen.pet.mapper.PetDetailMapper;
import com.imachen.pet.mapper.PetMapper;
import com.imachen.pet.mapper.PetOnlineAuditLogMapper;
import com.imachen.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    public void add(Pet pet) {
        super.add(pet);
        //添加宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.add(petDetail);
        }
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        //修改宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.update(petDetail);
        }
    }

    @Override
    public void delete(Long id) {
        super.delete(id);
        //删除宠物详情
        petDetailMapper.deleteByPetId(id);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        super.patchDelete(ids);
        //批量删除宠物详情
        petDetailMapper.patchDeleteByPetId(ids);

    }

    @Override
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        //1.对每一个宠物都要审核：审核name和resources["",多个数据]
        for (Long id:ids){
            Pet pet = petMapper.findOneById(id);
            //审核文本
            Boolean aBoolean = baiduAiAuditService.textAudit(pet.getName());
            //审核图片
            Boolean aBoolean1 = baiduAiAuditService.imageAudit(pet.getResources());

            PetOnlineAuditLog log = new PetOnlineAuditLog();
            log.setPetId(id);

            if (aBoolean && aBoolean1){
                //2.审核通过：改状态和时间 + 记录审核日志，状态改为1 - 上架
                petMapper.onsale(new Date(),id);
                log.setState(1);
                log.setNote("审核成功");
                petOnlineAuditLogMapper.add(log);
            }else {
                //3.审核失败：记录审核日志
                log.setState(0);//0表示审核失败
                log.setNote("审核失败，图片和文本非法!!!");//审核日志
                petOnlineAuditLogMapper.add(log);
                return AjaxResult.me().setSuccess(false).setMsg("审核失败：图片，文本非法，或图片不存在");
            }
        }
        return new AjaxResult();
    }

    @Override
    public void offsale(List<Long> ids, HttpServletRequest request) {
        //1.操作数据库：改下架时间=当前系统时间，改状态为0下架
        petMapper.offsale(new Date(),ids);
    }
}
